package A一周刷爆LeetCode.A基础班.F二叉树;

/**
 * @author wei.zhao
 * @description: 后继节点
 * @date 2022年02月28日 15:49
 */
public class Code11_SuccessorNode {

    public static void main(String[] args) {
        Node head = new Node(6);
        head.parent = null;
        head.left = new Node(3);
        head.left.parent = head;
        head.left.left = new Node(1);
        head.left.left.parent = head.left;
        head.left.left.right = new Node(2);
        head.left.left.right.parent = head.left.left;
        head.left.right = new Node(4);
        head.left.right.parent = head.left;
        head.left.right.right = new Node(5);
        head.left.right.right.parent = head.left.right;
        head.right = new Node(9);
        head.right.parent = head;
        head.right.left = new Node(8);
        head.right.left.parent = head.right;
        head.right.left.left = new Node(7);
        head.right.left.left.parent = head.right.left;
        head.right.right = new Node(10);
        head.right.right.parent = head.right;

        Node test = head.left.left;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.left.left.right;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.left;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.left.right;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.left.right.right;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.right.left.left;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.right.left;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.right;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.right.right; // 10's next is null
        System.out.println(test.value + " next: " + getSuccessorNode(test));
    }

    //获取后继节点
    private static Node getSuccessorNode(Node node) {
        if (node == null) {
            return null;
        }
        Node result = null;
        if (null != node.right) {
            // 右子节点不为空
            result = getSuccessorNodeRight(node);
        } else {
            // 右子节点为空，找第一个当前节点是父节点的左子节点，返回父节点
            while (node.parent != null) {
                if (node == node.parent.left) {
                    return node.parent;
                } else {
                    node = node.parent;
                }
            }
        }
        return result;
    }

    //获取右侧的后继节点
    public static Node getSuccessorNodeRight(Node node) {
        Node result = node.right;
        while (result.left != null) {
            result = result.left;
        }
        return result;
    }

    public static class Node {
        public int value;
        public Node left;
        public Node right;
        public Node parent;

        public Node(int data) {
            this.value = data;
        }
    }

}
